package com.xctz.niceman.developer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.b)
    private Button b;
    @ViewInject(R.id.tv)
    private TextView tv;
 /*   @ViewInject(R.id.lv)
    private ListView lv ;*/
    private String[] str = {"啦啦啦啦啦啦","哈哈哈哈哈哈","狗狗狗狗国故","楼楼哦路六"} ;
    private List<Fragment> f ;
    @ViewInject(R.id.fragment)
    private ViewPager vp ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        //进行关联
        x.view().inject(this);
//        Log.i("Log","lv的内存地址："+lv+"") ;
//        lv.setAdapter(new MyAdapter());
        f = new ArrayList<>() ;
        f.add(new Fragment1()) ;
        f.add(new Fragment2()) ;
        f.add(new Fragment3()) ;
      /*  FragmentManager manager = getSupportFragmentManager() ;
        FragmentTransaction transaction = manager.beginTransaction() ;
        Fragment1 f1 = new Fragment1() ;
        transaction.add(R.id.fragment,f1) ;
        transaction.commit() ;*/

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return f.get(position);
            }

            @Override
            public int getCount() {
                return f.size();
            }
        });

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //为按钮添加点击事件（type可以不写，默认为点击事件，其参数与其需实现的事件的参数一致）需要注意的一点就是方法名必须用private进行修饰
    @Event(type = View.OnClickListener.class, value = R.id.b)
    private void bClick(View v) {
        tv.setText("哈哈哈哈哈哈哈哈哈哈哈");
    }

/*
    //为多个按钮添加点击事件（type可以不写，默认为点击事件，其参数与其需实现的事件的参数一致）
    @Event(value={R.id.b,R.id.b2,R.id.b3})
    private void allClick(View v){
        switch (v.getId()){
            case R.id.b:
                tv.setText("哈哈哈哈哈哈哈哈");
                break ;
            case R.id.b2:
                tv.setText("啊啊啊啊啊啊啊");
                break ;
            case R.id.b3:
                tv.setText("吼吼吼吼吼吼吼");
                break ;
        }
    }
*/
    //为listview进行点击事件
    @Event(type=AdapterView.OnItemClickListener.class,value = R.id.lv)
    private void lvItemClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(MainActivity.this,"我是第"+position,Toast.LENGTH_SHORT).show();
        finish();
    }
    @Event(type=AdapterView.OnItemLongClickListener.class,value = R.id.lv)
    private boolean lvLongItemClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(MainActivity.this,"干嘛一直按人家啊"+position,Toast.LENGTH_SHORT).show();
        return false ;
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return str.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder vh = null ;
            if (v == null){
                v = getLayoutInflater().inflate(R.layout.lv_itwm,null) ;
                vh = new ViewHolder() ;
                //在这里面对布局进行绑定
                x.view().inject(vh,v);
                v.setTag(vh);
            }else {
                vh = (ViewHolder) v.getTag() ;
            }
            vh.sh.setText(str[position]+"");
            return v;
        }
    }
    class ViewHolder{
        @ViewInject(R.id.sh)
        TextView sh ;
    }

}
